use "Spain_POLAT_panel.dta", replace 


/*create new variables*/
gen female = sex
recode female 2=1 1=0
gen age10 = age/10
gen step1 = education
recode step1 1/3=0 4=1 5=2 6=3 7=4 8/9=5 10/11=6
gen educ = step1/6
gen hiinc = income
recode hiinc 5/10=1 .=0 *=0
gen loinc = income
recode loinc 1/3=1 .=0 *=0
gen pubsctr = publicworker
recode pubsctr 1=1 2=0 .=0
gen lrself10 = lrself/10
gen unemployed = sitlab
recode unemployed 4=1 5=1 *=0

gen econpres = ecosit
recode econpres 5=0 4=.25 3=.5 2=.75 1=1 
gen nret = ecositpast
recode nret 3=0 2=.5 1=1 
gen npros = ecositfut
recode npros 3=0 2=.5 1=1 

gen ptyvt2008 = vote2008
recode ptyvt2008 1=1 2=2 3=3 4=4 5=5 6=6 7=7 8=8 9=9 13=10 50=11 51=12 .=. 
label define ptyvt2008 1 "PSOE" 2 "PP" 3 "IU-ICV" 4 "CiU" 5 "PNV" 6 "UPyD" 7 "ERC" 8 "BNG" 9 "CC" 10 "NaBai" 11 "other" 12 "blancos", replace
label values ptyvt2008 ptyvt2008
gen pv3_2008 = ptyvt2008
recode pv3_2008 1=1 2=2 3/11=3 12=. *=.
gen ptyvt2011 = vote2011 
recode ptyvt2011 1=1 2=2 3=3 4=4 5=5 6=6 7=7 8=8 9=9 13=10 50=11 51=12 56=. .=.
label define ptyvt2011 1 "PSOE" 2 "PP" 3 "IU-ICV" 4 "CiU" 5 "PNV" 6 "UPyD" 7 "ERC" 8 "BNG" 9 "CC" 10 "NaBai" 11 "other" 12 "blancos", replace
label values ptyvt2011 ptyvt2011
gen pv3_2011 = ptyvt2011
recode pv3_2011 1=1 2=2 3/11=3 12=. *=.
label variable pv3_2011 "1 inc 2 expc 3 inexpc"
gen ptyvt2015 = vote2015 
recode ptyvt2015 1=1 2=2 3=3 4=4 5=5 6/50=6 51/52=7 54=8
label define ptyvt2015 1 "PSOE" 2 "PP" 3 "Podemos" 4 "Ciudadanos" 5 "IU/ICV" 6 "other" 7 "blank" 8 "don't recall", replace
label values ptyvt2015 ptyvt2015
gen pv3_2015 = ptyvt2015
recode pv3_2015 2=1 1=2 3=3 4=3 5=3 6=3 *=.
label variable pv3_2015 "1 inc 2 expc 3 inexpc"
gen ptyvt2016 = vote2016
recode ptyvt2016 1=1 2=2 3=3 4=4 5=5 7=6 6=7 9/50=7 51/54=8 56=9
label define ptyvt2016 1 "PSOE" 2 "PP" 3 "Podemos" 4 "Ciudadanos" 5 "IU/ICV" 6 "ERC" 7 "other" 8 "blank" 9 "don't recall", replace
label values ptyvt2016 ptyvt2016
gen pv3_2016 = ptyvt2016
recode pv3_2016 2=1 1=2 3=3 4=3 5=3 6=3 7=3 *=.
label variable pv3_2016 "1 inc 2 expc 3 inexpc"
gen ptyvt2019 = vote2019
recode ptyvt2019 1=1 2=2 3=3 4=4 7=5 23=6 27=7 6=8 8/18=8 25/26=8 28/50=8 51/52=9 53=10
label define ptyvt2019 1 "PSOE" 2 "PP" 3 "Podemos" 4 "Ciudadanos" 5 "ERC" 6 "Vox" 7 "PACMA" 8 "other" 9 "blank" 10 "don't recall", replace
label values ptyvt2019 ptyvt2019
gen pv3_2019 = ptyvt2019
recode pv3_2019 1=1 2=2 3=3 4=3 5=3 6=3 7=3 8=3 *=.
label variable pv3_2019 "1 inc 2 expc 3 inexpc"
gen ptyvt2019n = vote2019nov
recode ptyvt2019n 1=1 2=2 3=3 4=4 7=5 23=6 27=7 6=8 8/18=8 25/26=8 28/50=8 51/52=9 53=10
label values ptyvt2019n ptyvt2019
gen pv3_2019n = ptyvt2019n
recode pv3_2019n 1=1 2=2 3=3 4=3 5=3 6=3 7=3 8=3 *=.
label variable pv3_2019n "1 inc 2 expc 3 inexpc"

gen une2013q4 = ccaa
recode une2013q4 1=36.32 2=20.57 3=22.25 4=22.91 5=33.18 6=20.06 7=22.03 8=29.16 9=22.26 10=27.91 11=32.29 12=21.96 13=21.03 14=28.98 15=16.83 16=15.76 17=20.05 18=36.82 19=35.36
gen une2011q3 = ccaa
recode une2011q3 1=30.93 2=16.16 3=17.17 4=17.81 5=29.55 6=14.1 7=16.08 8=22.44 9=19.43 10=24.73 11=23.6 12=17.25 13=17.01 14=24.19 15=11.68 16=12.17 17=17.39 18=33.2 19=23.81
gen une2007q4 = ccaa
recode une2007q4 1=13.99 2=5.09 3=8.12 4=9.01 5=11.02 6=4.63 7=6.99 8=7.95 9=6.63 10=9.03 11=14.65 12=7.47 13=6.38 14=8.27 15=4.27 16=5.73 17=5.63 18=19.15 19=17.52
gen dune2013 = une2013q4 - une2011q3 
gen dune2011 = une2011q3 - une2007q4

/*create cf estimator*/
reg econpres age sex i.education income i.polintr i.sitlab i.ecositpersonal
predict econpres_r, residuals
pwcorr econpres econpres_r

/*transform data for analysis*/
keep zn ccaa une* dune* une2011q3 jobstatus female age10 step1 educ hiinc loinc pubsctr lrself10 econpres  econpres_r nret npros ecoc* voteintentionspain ptyvt2008 ptyvt2011 ptyvt2015 ptyvt2016 ptyvt2019 ptyvt2019n pv3_2008 pv3_2011 pv3_2015 pv3_2016 pv3_2019 pv3_2019n codpanelista2 wave

reshape wide zn ccaa une* dune* jobstatus female age10 step1 educ hiinc loinc pubsctr lrself10 econpres econpres_r nret npros ecoc* voteintentionspain ptyvt2008 ptyvt2011 ptyvt2015 ptyvt2016 ptyvt2019 ptyvt2019n pv3_2008 pv3_2011 pv3_2015 pv3_2016 pv3_2019 pv3_2019n, i(codpanelista2) j(wave)

gen vPSOE2008 = 0
gen vPP2008 = 0
gen vPSOE2011 = 0
gen vPP2011 = 0
recode vPSOE2008 *=1 if ptyvt20081==1 | ptyvt20082==1 | ptyvt20083==1 | ptyvt20084==1 
recode vPP2008 *=1 if ptyvt20081==2 | ptyvt20082==2 | ptyvt20083==2 | ptyvt20084==2 
recode vPSOE2011 *=1 if ptyvt20114==1 | ptyvt20115==1 
recode vPP2011 *=1 if ptyvt20114==2 | ptyvt20115==2
gen econpres2011 = (econpres1 + econpres2 + econpres3 + econpres4)/4
gen econpres2015 = (econpres5 + econpres6 + econpres7 + econpres8)/4
gen econpres67 = econpres7
recode econpres67 .=0 if econpres6==0
recode econpres67 .=.25 if econpres6==.25
recode econpres67 .=.5 if econpres6==.5
recode econpres67 .=.75 if econpres6==.75
recode econpres67 .=1 if econpres6==1
gen econpres3r = econpres3
recode econpres3r 0=1 .25=.75 .75=.25 1=0 
gen econpres7r = econpres7
recode econpres7r 0=1 .25=.75 .75=.25 1=0 
gen econpres67r = econpres7
recode econpres67r 0=1 .25=.75 .75=.25 1=0 

/*Main Model reported in Table 2 and Figure 1 - baseline is incumbent - PSOE in 2011, PP in 2015*/
mlogit pv3_20115 c.econpres3 vPSOE2008 vPP2008 lrself105 female5 age105 educ5 hiinc5 pubsctr5, b(1)
qui mlogit pv3_20115 c.econpres3r vPSOE2008 vPP2008 lrself105 female5 age105 educ5 hiinc5 pubsctr5, b(1)
margins, dydx(econpres3r)
marginsplot, yline(0, lc(gs11) lpattern(dash)) title("A. 2011 Election") xtitle("Party choice") scheme(s1mono) name(m2011, replace) 
mlogit pv3_20158 c.econpres67 vPSOE2011 vPP2011 lrself108 female8 age108 educ8 hiinc8 pubsctr8, b(1)
qui mlogit pv3_20158 c.econpres67r vPSOE2011 vPP2011 lrself108 female8 age108 educ8 hiinc8 pubsctr8, b(1)
margins, dydx(econpres67r)
marginsplot, yline(0, lc(gs11) lpattern(dash)) title("B. 2015 Election") xtitle("Party choice") scheme(s1mono) name(m2015, replace)
graph combine m2011 m2015, ycommon scheme(s1mono)

/*catalonia and PV only Model in Table A2 and Figure A2 of SI*/
mlogit pv3_20115 c.econpres3 female5 age105 educ5 pubsctr5 if ccaa5==9 | ccaa5==16 , b(1)
qui mlogit pv3_20115 c.econpres3r female5 age105 educ5 pubsctr5 if ccaa5==9 | ccaa5==16, b(1)
margins, dydx(econpres3r) 
marginsplot, yline(0, lc(gs11) lpattern(dash)) title("A. 2011 Election") xtitle("Party choice") scheme(s1mono) name(m2011s, replace) 
mlogit pv3_20158 c.econpres67 female8 age108 educ8 pubsctr8 if ccaa8==9 | ccaa8==16 , b(1)
qui mlogit pv3_20158 c.econpres67r female8 age108 educ8 pubsctr8 if ccaa8==9 | ccaa8==16 , b(1)
margins, dydx(econpres67r) 
marginsplot, yline(0, lc(gs11) lpattern(dash)) title("B. 2015 Election") xtitle("Party choice") scheme(s1mono) name(m2015s, replace)
graph combine m2011s m2015s, ycommon scheme(s1mono)

/*endogeneity robustness model with CF estimator - reported in Table A1 of SI*/
bootstrap, reps(200): mlogit pv3_20115 c.econpres_r3 vPSOE2008 vPP2008 lrself105 female5 age105 educ5 hiinc5 pubsctr5, b(1)
bootstrap, reps(200): mlogit pv3_20158 c.econpres_r8 vPSOE2011 vPP2011 lrself108 female8 age108 educ8 hiinc8 pubsctr8, b(1)
